package kku.cs.fgl.actor;

import kku.cs.fgl.Actor;
import kku.cs.fgl.GamePane;
import kku.cs.fgl.SpriteCell;
import kku.cs.fgl.SpriteGroup;
import kku.cs.fgl.SpriteManager;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

public class SpriteActor extends Actor {

	private SpriteGroup cells;

	private int cur_frame = 0;

	private int direction = 1;

	private int delay = 100;

	private boolean hflip = false;

	private boolean vflip = false;

	private boolean animate = true;

	private String keyName = "";

	private SpriteManager sprites;

	int ftime = 0;

	public SpriteActor() {
		super();
	}

	public SpriteActor(SpriteManager sprites) {
		super();
		this.sprites = sprites;
	}

	public void calculateSpeed(int time) {
		if (!animate)
			return;

		if (cells == null && sprites != null && keyName != null) {
			setKeyName(keyName);
		}
		int delay = cells.get(cur_frame).getDelay();
		if (delay <= 0) {
			delay = this.delay;
		}
		ftime += time;
		if (ftime > delay) {
			ftime = 0;
			stepFrame(1);
		}
		if (cells != null) {
			setWidth(cells.get(cur_frame).getWidth());
			setHeight(cells.get(cur_frame).getHeight());
		}
	}

	public boolean contains(float x2, float y2) {
		boolean t = super.contains(x2, y2);
		if (t && getFrameCount() > 0) {
			int cx = (int) ((x2 - getX()) / getScalex());
			int cy = (int) ((y2 - getY()) / getScaley());
			Color c = cells.get(cur_frame).getPixel(cx, cy);
			t = c.a > 0.1f;
		}
		return t;
	}

	public SpriteGroup.SFrame getCell() {
		SpriteGroup.SFrame cell = null;
		if (getFrameCount() > 0 && cur_frame < getFrameCount()) {
			cell = cells.get(cur_frame);
		}
		return cell;
	}

	public SpriteGroup getCells() {
		return cells;
	}

	public int getCur_frame() {
		return cur_frame;
	}

	public int getDelay() {
		return delay;
	}

	/**
	 * @return Returns the direction.
	 */
	public int getDirection() {
		return direction;
	}

	public int getFrameCount() {
		if (cells == null)
			return 0;
		return cells.size();
	}

	public String getKeyName() {
		return keyName;
	}

	public SpriteManager getSprites() {
		return sprites;
	}

	public boolean isAnimate() {
		return animate;
	}

	/**
	 * @return Returns the hflip.
	 */
	public boolean isHflip() {
		return hflip;
	}

	/**
	 * @return Returns the vflip.
	 */
	public boolean isVflip() {
		return vflip;
	}

	public void paint(Graphics g) {
		if (getFrameCount() > 0) {
			cells.get(cur_frame).paint(g, 0, 0, hflip, vflip, getFilter());
			int delay = cells.get(cur_frame).getDelay();
			// g.setFont(GamePane.getFont(0));
			// g.drawString("delay="+delay,0,0);
			// GamePane.getFont(0).drawString(0,0,"delay="+delay,Color.white);
		}
	}

	public void setAnimate(boolean animate) {
		this.animate = animate;
	}

	public void setCells(SpriteGroup cells) {
		cur_frame = 0;
		this.cells = cells;
	}

	public void setCur_frame(int cur_frame) {
		if (cur_frame >= 0 && cur_frame < getFrameCount())
			this.cur_frame = cur_frame;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}

	/**
	 * @param direction
	 *            The direction to set.
	 */
	public void setDirection(int direction) {
		this.direction = direction;
	}

	/**
	 * @param hflip
	 *            The hflip to set.
	 */
	public void setHflip(boolean hflip) {
		this.hflip = hflip;
	}

	public void setKeyName(String keyName) {
		if (sprites != null && !keyName.equals(this.keyName)) {
			this.keyName = keyName;
			cells = sprites.getGroup(keyName);
			cur_frame = 0;
		}
	}

	public void setSprites(SpriteManager sprites) {
		this.sprites = sprites;
		if (sprites != null && keyName != null)
			cells = sprites.getGroup(keyName);
	}

	/**
	 * @param vflip
	 *            The vflip to set.
	 */
	public void setVflip(boolean vflip) {
		this.vflip = vflip;
	}

	public void stepFrame(int step) {
		if (direction == 1) {
			cur_frame += step;
			if (cur_frame >= getFrameCount())
				cur_frame = 0;
		} else {
			cur_frame -= step;
			if (cur_frame < 0)
				cur_frame = getFrameCount() - 1;
		}
	}
}